//
//  LeftViewController.m
//  Menu
//
//  Created by dllo on 15/7/3.
//  Copyright (c) 2015年 lijiangang. All rights reserved.
//

#import "LeftViewController.h"

@interface LeftViewController ()

@property(strong, readwrite, nonatomic)UITableView *leftTableView;

@end

@implementation LeftViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.leftTableView =
    ({
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height - 54 * 5) / 2.0f, self.view.frame.size.width, 54 * 5) style:UITableViewStylePlain];
        tableView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.opaque = NO;
        tableView.backgroundColor = [UIColor clearColor];
       
        tableView.backgroundView = nil;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.bounces = NO;
        tableView;
    });
    [self.view addSubview:self.leftTableView];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellMark = @"theCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellMark];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellMark];
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:21];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.highlightedTextColor = [UIColor lightGrayColor];
        cell.selectedBackgroundView = [[UIView alloc] init];
    }
    NSArray *titlesArray = @[ @"厨房必备", @"查看收藏", @"翻滚吧美食", @"清除缓存"];
    cell.textLabel.text = titlesArray[indexPath.row];
    cell.textLabel.textColor = [UIColor blackColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.contentView.backgroundColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor clearColor];
    cell.contentView.alpha = 0.6;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSLog(@"%@",[defaults objectForKey:@"number"]);
    
    if ([[defaults objectForKey:@"number"]isEqualToString:@"4"])
    {
        if (indexPath.row == 0)
        {
            [self.delegate4 pushToEssentialController];
            [self.sideMenuViewController hideMenuViewController];
        }
        if (indexPath.row == 1)
        {
            [self.delegate4 pushToCollectController];
            [self.sideMenuViewController hideMenuViewController];
        }
        if (indexPath.row == 2)
        {
            [self.delegate4 pushToRandomController];
            [self.sideMenuViewController hideMenuViewController];
        }
        if (indexPath.row == 3)
        {
            NSUInteger size = [[SDImageCache sharedImageCache]getSize];
            CGFloat mb = size / 1024.0 / 1024.0;
            NSString *string = [NSString stringWithFormat:@"当前缓存为%.2fMB",mb];
            NSLog(@"%@",string);
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:string message:@"是否清除?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
            [alert release];
        }
        
    }
    
    if ([[defaults objectForKey:@"number"]isEqualToString:@"3"])
    {
        if (indexPath.row == 0)
        {
            [self.delegate3 pushToEssentialController];
            [self.sideMenuViewController hideMenuViewController];
        }
        if (indexPath.row == 1)
        {
            [self.delegate3 pushToCollectController];
            [self.sideMenuViewController hideMenuViewController];
        }
        if (indexPath.row == 2)
        {
            [self.delegate3 pushToRandomController];
            [self.sideMenuViewController hideMenuViewController];
        }
        if (indexPath.row == 3)
        {
            NSUInteger size = [[SDImageCache sharedImageCache]getSize];
            CGFloat mb = size / 1024.0 / 1024.0;
            NSString *string = [NSString stringWithFormat:@"当前缓存为%.2fMB",mb];
            NSLog(@"%@",string);
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:string message:@"是否清除?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
            [alert release];
        }
       
    }
    
    if ([[defaults objectForKey:@"number"]isEqualToString:@"2"])
    {
        if (indexPath.row == 0)
        {
            [self.delegate2 pushToEssentialController];
            [self.sideMenuViewController hideMenuViewController];
        }
        if (indexPath.row == 1)
        {
            [self.delegate2 pushToCollectController];
            [self.sideMenuViewController hideMenuViewController];
        }
        if (indexPath.row == 2)
        {
            [self.delegate2 pushToRandomController];
            [self.sideMenuViewController hideMenuViewController];
        }
        if (indexPath.row == 3)
        {
            NSUInteger size = [[SDImageCache sharedImageCache]getSize];
            CGFloat mb = size / 1024.0 / 1024.0;
            NSString *string = [NSString stringWithFormat:@"当前缓存为%.2fMB",mb];
            NSLog(@"%@",string);
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:string message:@"是否清除?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
            [alert release];
        }
        
    }
    
    if([[defaults objectForKey:@"number"]isEqualToString:@"1"])
    {
        if (indexPath.row == 0)
        {
            [self.myDelegate pushToEssentialController];
            [self.sideMenuViewController hideMenuViewController];
        }
        if (indexPath.row == 1)
        {
            [self.myDelegate pushToCollectController];
            [self.sideMenuViewController hideMenuViewController];
        }
        if (indexPath.row == 2)
        {
            [self.myDelegate pushToRandomController];
            [self.sideMenuViewController hideMenuViewController];
        }
        if (indexPath.row == 3)
        {
            NSUInteger size = [[SDImageCache sharedImageCache]getSize];
            CGFloat mb = size / 1024.0 / 1024.0;
            NSString *string = [NSString stringWithFormat:@"当前缓存为%.2fMB",mb];
            NSLog(@"%@",string);
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:string message:@"是否清除?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
            [alert release];
        }
    }
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1)
    {
        [[SDImageCache sharedImageCache] clearDisk];
        [[DataHandle shareData] openDataBase];
        [[DataHandle shareData] dropCaudleCacheTable];
        [[DataHandle shareData] dropCaudleListCacheTable];
        [[DataHandle shareData] dropGoodCacheTable];
        [[DataHandle shareData] dropHomeCacheTable];
        [[DataHandle shareData] dropNewsCacheTable];
        [[DataHandle shareData] dropSearchHistoryTable];
        //[[DataHandle shareData] dropTable];
    }
}































- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
